home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap20 / BigJob1 / BigJob1.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.5 KB  |  161 lines

  1. /*----------------------------------------
  2.    BIGJOB1.C -- Multithreading Demo
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8. #include <process.h>
  9.  
  10. #define REP              1000000
  11.  
  12. #define STATUS_READY     0
  13. #define STATUS_WORKING   1
  14. #define STATUS_DONE      2
  15.  
  16. #define WM_CALC_DONE     (WM_USER + 0)
  17. #define WM_CALC_ABORTED  (WM_USER + 1)
  18.  
  19. typedef struct
  20. {
  21.      HWND hwnd ;
  22.      BOOL bContinue ;
  23. }
  24. PARAMS, *PPARAMS ;
  25.  
  26. LRESULT APIENTRY WndProc (HWND, UINT, WPARAM, LPARAM) ;
  27.  
  28. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29.                     PSTR szCmdLine, int iCmdShow)
  30. {
  31.      static TCHAR szAppName[] = TEXT ("BigJob1") ;
  32.      HWND         hwnd ;
  33.      MSG          msg ;
  34.      WNDCLASS     wndclass ;
  35.      
  36.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  37.      wndclass.lpfnWndProc   = WndProc ;
  38.      wndclass.cbClsExtra    = 0 ;
  39.      wndclass.cbWndExtra    = 0 ;
  40.      wndclass.hInstance     = hInstance ;
  41.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  42.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  43.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  44.      wndclass.lpszMenuName  = NULL ;
  45.      wndclass.lpszClassName = szAppName ;
  46.      
  47.      if (!RegisterClass (&wndclass))
  48.      {
  49.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  50.                       szAppName, MB_ICONERROR) ;
  51.           return 0 ;
  52.      }
  53.      
  54.      hwnd = CreateWindow (szAppName, TEXT ("Multithreading Demo"),
  55.                           WS_OVERLAPPEDWINDOW,
  56.                           CW_USEDEFAULT, CW_USEDEFAULT,
  57.                           CW_USEDEFAULT, CW_USEDEFAULT,
  58.                           NULL, NULL, hInstance, NULL) ;
  59.      
  60.      ShowWindow (hwnd, iCmdShow) ;
  61.      UpdateWindow (hwnd) ;
  62.      
  63.      while (GetMessage (&msg, NULL, 0, 0))
  64.      {
  65.           TranslateMessage (&msg) ;
  66.           DispatchMessage (&msg) ;
  67.      }
  68.      return msg.wParam ;
  69. }
  70.  
  71. void Thread (PVOID pvoid)
  72. {
  73.      double           A = 1.0 ;
  74.      INT              i ;
  75.      LONG             lTime ;
  76.      volatile PPARAMS pparams ;
  77.      
  78.      pparams = (PPARAMS) pvoid ;
  79.      
  80.      lTime = GetCurrentTime () ;
  81.      
  82.      for (i = 0 ; i < REP && pparams->bContinue ; i++)
  83.           A = tan (atan (exp (log (sqrt (A * A))))) + 1.0 ;
  84.      
  85.      if (i == REP)
  86.      {
  87.           lTime = GetCurrentTime () - lTime ;
  88.           SendMessage (pparams->hwnd, WM_CALC_DONE, 0, lTime) ;
  89.      }
  90.      else
  91.           SendMessage (pparams->hwnd, WM_CALC_ABORTED, 0, 0) ;
  92.      
  93.      _endthread () ;
  94. }
  95.  
  96. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  97. {
  98.      static INT     iStatus ;
  99.      static LONG    lTime ;
  100.      static PARAMS  params ;
  101.      static TCHAR * szMessage[] = { TEXT ("Ready (left mouse button begins)"),
  102.                                     TEXT ("Working (right mouse button ends)"),
  103.                                     TEXT ("%d repetitions in %ld msec") } ;
  104.      HDC            hdc ;
  105.      PAINTSTRUCT    ps ;
  106.      RECT           rect ;
  107.      TCHAR          szBuffer[64] ;
  108.      
  109.      switch (message)
  110.      {
  111.      case WM_LBUTTONDOWN:
  112.           if (iStatus == STATUS_WORKING)
  113.           {
  114.                MessageBeep (0) ;
  115.                return 0 ;
  116.           }
  117.           
  118.           iStatus = STATUS_WORKING ;
  119.           
  120.           params.hwnd = hwnd ;
  121.           params.bContinue = TRUE ;
  122.           
  123.           _beginthread (Thread, 0, ¶ms) ;
  124.           
  125.           InvalidateRect (hwnd, NULL, TRUE) ;
  126.           return 0 ;
  127.           
  128.      case WM_RBUTTONDOWN:
  129.           params.bContinue = FALSE ;
  130.           return 0 ;
  131.           
  132.      case WM_CALC_DONE:
  133.           lTime = lParam ;
  134.           iStatus = STATUS_DONE ;
  135.           InvalidateRect (hwnd, NULL, TRUE) ;
  136.           return 0 ;
  137.           
  138.      case WM_CALC_ABORTED:
  139.           iStatus = STATUS_READY ;
  140.           InvalidateRect (hwnd, NULL, TRUE) ;
  141.           return 0 ;
  142.           
  143.      case WM_PAINT:
  144.           hdc = BeginPaint (hwnd, &ps) ;
  145.           
  146.           GetClientRect (hwnd, &rect) ;
  147.           
  148.           wsprintf (szBuffer, szMessage[iStatus], REP, lTime) ;
  149.           DrawText (hdc, szBuffer, -1, &rect,
  150.                     DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
  151.           
  152.           EndPaint (hwnd, &ps) ;
  153.           return 0 ;
  154.           
  155.      case WM_DESTROY:
  156.           PostQuitMessage (0) ;
  157.           return 0 ;
  158.      }
  159.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  160. }
  161.